<?php
class Company_model extends MY_Model
{
    private $table = 'yy_company';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_company_by_id($id)
    {
        $this->db->where("companyid",$id);
        $query=$this->db->get($this->table);
        return $query->row_array();
    }
    public function get_cat_by_parent_id($parent_id)
    {
        $this->db->where("parent_id",$parent_id);
        $query=$this->db->get($this->table);
        return $query->result_array();
    }
    public function get_company_list()
    {

        $sql = "SELECT * FROM yy_company WHERE isDel=0 ORDER BY addtime DESC";
        $query = $this->db->query($sql);
        return $query->result_array();        
    }
    public function get_area_list($companyid)
    {

		$sql = "SELECT * FROM yy_company_contact WHERE isDel=0 AND companyid={$companyid} ORDER BY id DESC";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    public function add_company($data)
    {
        $res= $this->db->insert($this->table,$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }
    public function update_contact($id,$data)
    {
        $this->db->where('id',$id);
        return $this->db->update("yy_company_contact",$data);
    }    


     public function add_contact($data)
    {
        $res= $this->db->insert("yy_company_contact",$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }
    public function edit_company($id,$data)
    {
        $this->db->where('companyid',$id);
        return $this->db->update($this->table,$data);
    } 


    //删除分类
    public function del_company($companyid)
    {
        $this->db->where('companyid',$companyid);
        return $this->db->update($this->table,array("isDel"=>1));
    }
    //删除公司地区联系地址
    public function del_address($address_id)
    {
		$this->db->where('id',$address_id);
        return $this->db->delete("yy_company_contact");
    }
    /*
     * 获取无限分类ID下面的子类ID集
     *
    */
    public function get_Children_Ids ($cat_id)
    {    
        $ids = "";
		$sql = "SELECT * FROM yy_item_category WHERE parent_id='{$cat_id}'";
		$query = $this->db->query($sql);
        $rs = $query->result_array ();
        if ($rs){ 
            foreach ($rs as $k=>$row){
                $ids .= ','.$row['cat_id'];
                $ids .= $this->get_Children_Ids ($row['cat_id']);
            }            
        }        
        return $ids;
    }   
    
    /**
     * 获取获取当前分类及所有父分类名字
     *@author KevinBai
     */
    public function get_level_catname_bycid($parent_id){
       $sql = "SELECT * FROM yy_item_category WHERE cat_id='{$parent_id}'";
       $query = $this->db->query($sql);
       $tem = $query->row_array();
       $pre_name = "";
       if($query->num_rows()){
           $pre_name.= $this->get_level_catname_bycid($tem['parent_id']); 
           $pre_name.= $tem['cat_name'].'->';   
        }
        return $pre_name;
    }       
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */
